package com.siecomzg.transport.handler;

import android.os.Bundle;

import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;

import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created on 2017/1/19.
 */

public class KeyBoardHandler extends CommonHandler {

    public static final String TAG  = KeyBoardHandler.class.getSimpleName();
    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {
        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if (callbackTaskHashMap.containsKey(msgid)) {
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            DeviceCallback callback = callbackTask.getCallback();
            callbackTask.getTimer().cancel();
            boolean isOnMain = callbackTask.isOnMain();

            int respondCmd = msg.getCmd();
            if (respondCmd == ConstDefine.INPUT_MAIN_RESPOND) {
                inputMainReceive(msg, callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if (respondCmd == ConstDefine.INPUT_WORK_RESPOND) {
                inputWorkReceive(msg, callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }

            //用传入的公钥加密主密钥 并回传主密钥密文
            if(respondCmd==ConstDefine.SM2_RSA_MAINKEY_RESPOND){
                LogUtil.e(TAG,"SM2_RSA_MAINKEY_RESPOND = " + ConstDefine.SM2_RSA_MAINKEY_RESPOND);
                doProcessAllBackMsg(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }

            if (respondCmd == ConstDefine.INPUT_KEY_RESPOND) {
                inputKeyReceive(msg, callback, isOnMain);
            }
            if (respondCmd == ConstDefine.SET_ENCRYPT_RESPOND) {
                setEncryptReceive(msg, callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.KEYBOARD_CANCEL_RESPOND){
                cancelCall(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.TMK_RESPOND){
                LogUtil.e("TMK","respond");
                inputFinish(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.TMK_DECRYPT_RESPOND){
                LogUtil.e("TMK_DECRYPT_RESPOND","respond");
                inputMainByOld(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.TMK_OLD_RESPOND){
                LogUtil.e("TMK_OLD_RESPOND","respond");
                updateMainByOld(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.RESET_KEYBOARD_RESPOND){
                LogUtil.e("RESET_KEYBOARD_RESPOND","respond");
                resetOk(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if(respondCmd==ConstDefine.CALCMAC_RESPOND){
                LogUtil.e("CALCMAC_RESPOND","respond");
                calcMAC(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }


        }

    }

    private void doProcessAllBackMsg(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void calcMAC(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void resetOk(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void updateMainByOld(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    private void inputMainByOld(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    private void inputFinish(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    private void cancelCall(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        CallbackMap.getCallbackTaskHashMap().remove(msg.getMsgid());
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    public void setEncryptReceive(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);

        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    public void inputKeyReceive(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {

            try {
                JSONObject jsonObject = new JSONObject(msg.getRespond());
                if (jsonObject.getInt("flag") == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("data", jsonObject.getInt("data"));
                    SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
                }
                if (jsonObject.getInt("flag") == 0) {
                    Bundle bundle = new Bundle();
                    bundle.putString("data", jsonObject.getString("data"));
                    SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, bundle, isOnMain);
                    CallbackMap.getCallbackTaskHashMap().remove(msg.getMsgid());

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
            CallbackMap.getCallbackTaskHashMap().remove(msg.getMsgid());
        }


    }

    public void inputWorkReceive(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {

        int errno = msg.getError();
        if (errno == 0) {
            try {
                JSONObject jsonObject = new JSONObject(msg.getRespond());
                Bundle bundle = new Bundle();
                bundle.putString("data", jsonObject.getString("data"));
                SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }

    public void inputMainReceive(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            try {
                JSONObject jsonObject = new JSONObject(msg.getRespond());
                Bundle bundle = new Bundle();
                bundle.putString("data", jsonObject.getString("data"));
                SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }


}
